package com.lanxin.jet.services.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.lanxin.jet.beans.ContentBean;
import com.lanxin.jet.mappers.ContentMapper;
import com.lanxin.jet.services.ContentService;
import com.lanxin.jet.tools.JetCriteria;
import com.lanxin.jet.tools.JetDate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ContentServiceImpl implements ContentService {

    @Autowired
    private ContentMapper contentMapper;

    @Override
    public String insert(String fid, String title) {
        int sort = 1;
        Map<String, Integer> sortMap = contentMapper.findNewSort(fid);
        if (sortMap != null) {
            sort += sortMap.get("sort");
        }
        String data = "{}";
        if (!fid.isEmpty() && fid.length() == 13) {
            ContentBean parentContent = contentMapper.query(fid);
            if (parentContent != null) {
                data = parentContent.getData();
            }
        }
        ContentBean content = new ContentBean();
        content.setId(JetDate.uniqid());
        content.setFid(fid);
        content.setTitle(title);
        content.setData(data);
        content.setCreateTime(JetDate.timeNow());
        content.setSort(sort);
        content.setStatus(STATUS_STAY);
        contentMapper.insert(content);
        return content.getId();
    }

    @Override
    public int count(String fid, String keyword) {
        JetCriteria criteria = new JetCriteria();
        criteria.addCondition("fid='"+fid+"'", JetCriteria.OPT_AND);
        criteria.addLike("title", keyword);
        return contentMapper.count(criteria);
    }

    @Override
    public ContentBean get(String id) {
        return contentMapper.query(id);
    }

    @Override
    public void update(ContentBean content) {
        content.setChangeTime(JetDate.timeNow());
        contentMapper.update(content);
    }

    @Override
    public void delete(String id) {
        contentMapper.delete(id);
    }

    @Override
    public List<String> getBreadcrumb(String id, int level) {
        ArrayList<String> array = new ArrayList<>();
        ContentBean content = contentMapper.query(id);
        if (content != null) {
            array.add(content.getId() + "^" + content.getTitle());
            while(--level > 0) {
                content = contentMapper.query(content.getFid());
                if (content == null) {
                    break;
                }
                array.add(0, content.getId() + "^" + content.getTitle());
            }
        }
        return array;
    }

    @Override
    public List<ContentBean> getSimpleList(int offset, int limit, String fid, String keyword) {
        JetCriteria criteria = new JetCriteria();
        criteria.setSelect("id,fid,cover,title,keyword,data,sort,createTime,changeTime,status");
        criteria.addCondition("fid='"+fid+"'", JetCriteria.OPT_AND);
        criteria.addLike("title", keyword);
        criteria.setPage(offset, limit);
        criteria.orderBy("sort", JetCriteria.ORDER_DESC);
        return contentMapper.queryList(criteria);
    }

}